
from saas.const import *
import time

fd = None

def log_open( log_file ):
	global fd
	fd = open( log_file , 'a' , 0 )

def log_close():
	global fd
	fd.close()


def logm( level , msg ):
	global fd

	lstr = ''
	if level == LOG_DEBUG:
		lstr = 'DEBUG'
	elif level == LOG_INFO:
		lstr = 'INFO '
	elif level == LOG_WARNING:
		lstr = 'WARN '
	elif level == LOG_ERR:
		lstr = 'ERROR'
	else:
		lstr = '?????'

	tdata = time.gmtime( time.time() )

	fd.write( '%04d-%02d-%02d %02d:%02d:%02d UTC :: %s :: %s\n' % ( tdata[0] , tdata[1] , tdata[2] , tdata[3] , tdata[4] , tdata[5] , lstr , msg ) )


def saaserror( ret ):
	if ret == SAAS_OP_SUCCESS:  return "(%d) Success" % (ret)

	if ret == SAAS_OP_INVALID_PASSWORD:  return "(%d) Invalid Password" % (ret)
	if ret == SAAS_OP_USER_NOT_FOUND:  return "(%d) User Not Found" % (ret)
	if ret == SAAS_OP_GROUP_NOT_FOUND:  return "(%d) Group Not Found" % (ret)
	if ret == SAAS_OP_USER_UNAUTHORISED:  return "(%d) User Unauthorised" % (ret)
	if ret == SAAS_OP_USERNAME_TOO_LONG:  return "(%d) Username Too Long" % (ret)

	if ret == SAAS_OP_SERVER_ERROR:  return "(%d) Server Error" % (ret)
	if ret == SAAS_OP_NO_HOSTS:  return "(%d) No Usable Hosts" % (ret)
	if ret == SAAS_OP_DECRYPT_ERROR:  return "(%d) Decryption Error" % (ret)
	if ret == SAAS_OP_MAGIC_ERROR:  return "(%d) Magic Error" % (ret)
	if ret == SAAS_OP_FILE_ERROR:  return "(%d) File Error" % (ret)
	if ret == SAAS_OP_PAD_ERROR:  return "(%d) Padding Error" % (ret)
	if ret == SAAS_OP_PARSE_ERROR:  return "(%d) Parsing Error" % (ret)
	if ret == SAAS_OP_ARG_ERROR:  return "(%d) Argument Error" % (ret)
	if ret == SAAS_OP_MEM_ERROR:  return "(%d) Memory Error" % (ret)
	if ret == SAAS_OP_TIMEOUT_ERROR:  return "(%d) Timeout" % (ret)
	if ret == SAAS_OP_SOCKET_ERROR:  return "(%d) Socket Error" % (ret)
	if ret == SAAS_OP_MALFORMED_REQUEST:  return "(%d) Malformed Request" % (ret)
	if ret == SAAS_OP_MALFORMED_RESPONSE:  return "(%d) Malformed Response" % (ret)

	return "(%d) Unknown" % (ret)



